/**
 * 
 */
package com.orange.controller.school;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.controller.BaseController;
import com.orange.dbhelp.page.Page;
import com.orange.handler.AuthApplyFileService;
import com.orange.handler.AuthExportGroupService;
import com.orange.handler.AuthMoneyResourceDataService;
import com.orange.handler.AuthResourceDataService;
import com.orange.handler.AuthSchoolItemMapService;
import com.orange.handler.AuthSchoolService;
import com.orange.handler.AuthStudentDataService;
import com.orange.handler.AuthTeachResourceService;
import com.orange.handler.AuthTeacherDataService;
import com.orange.modules.bean.SessionUser;
import com.orange.modules.entity.expert.AuthGroup;
import com.orange.modules.entity.school.AuthSchool;
import com.orange.util.Hash;
import com.orange.util.ReturnCode;

/**
 * @author gaogs
 *
 *         2016-10-16
 */
@Controller
@RequestMapping("/school")
public class AuthSchoolController extends BaseController {

	private static final Logger log = Logger
			.getLogger(AuthSchoolController.class);

	@Autowired
	private AuthSchoolService authSchoolService;

	@Autowired
	private AuthApplyFileService authApplyFileService;

	@Autowired
	private AuthSchoolItemMapService authSchoolItemMapService;

	
	@Autowired
	private AuthStudentDataService authStudentDataService;

	@Autowired
	private AuthTeacherDataService authTeacherDataService;

	@Autowired
	private AuthResourceDataService authResourceDataService;

	@Autowired
	private AuthTeachResourceService authTeachResourceService;

	@Autowired
	private AuthMoneyResourceDataService authMoneyResourceDataService;
	
	@Autowired
	private AuthExportGroupService authExportGroupService;

	/**
	 * 新增机构
	 * 
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/insertAuthSchool", produces = MediaType.APPLICATION_JSON_VALUE
			+ ";charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> insertAuthSchool(HttpServletRequest request,
			@RequestBody Map<String, String> map) throws SQLException {
		try {
			String account = map.get("account");
			String schoolname = map.get("schoolname");
			String major = map.get("major");
			if (StringUtils.isBlank(account)) {
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL,"机构代码不能为空");
			}
			if (StringUtils.isBlank(schoolname)) {
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL,"机构名不能为空");
			}
			if (StringUtils.isBlank(major)) {
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL,"专业不可为空");
			}
			AuthSchool au=authSchoolService.queryAuthSchoolInfoByAccount(account);
			if(null!=au){
				return toResponse(null, ReturnCode.USER_HAVE_EXIST,"该机构代码已存在");
			}
			AuthSchool as = new AuthSchool();
			String nowTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			as.setAccount(account);
			as.setMajor(major);
			as.setCreateTime(nowTime);
			String newPassword=Hash.sha2InHex("123456");
			as.setPassword(newPassword);
			as.setSchoolName(schoolname);
			as.setReview("0");
			as.setState("0");
			as.setType("0");
			as.setUpdateTime(nowTime);
			as.setScience("0");
			as.setLiberalarts("0");
			as.setWholeDeclaration("0");
			as.setFpexpert("0");
			authSchoolService.updateAuthSchoolInfo(as);
			return toResponse(as, ReturnCode.SUCCESS, "新增机构成功");
		} catch (Exception e) {
			log.info("新增机构失败", e);
			return toResponse(null, ReturnCode.FAIL, "新增机构失败");
		}
	}

	/**
	 * 修改是否现场考察状态
	 * 
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/updateAuthSchool", produces = MediaType.APPLICATION_JSON_VALUE
			+ ";charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> updateAuthSchool(HttpServletRequest request,
			@RequestParam String pid ,@RequestParam String review) throws SQLException {

		try {
			if (StringUtils.isBlank(pid)) {
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "ID不能为空");
			}
			if (StringUtils.isBlank(review)) {
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "考察状态不能为空");
			}
			AuthSchool as = authSchoolService.queryAuthSchoolInfoBypid(pid);
			if(null==as){
				return toResponse(null, ReturnCode.USERINFO_NOT_EXIST, "该机构信息不存在");
			}
			if(!"0".equals(review)&&!"1".equals(review)){
				return toResponse(null, ReturnCode.FAIL, "别瞎定义状态");
			}
			if("0".equals(review)){//修改为不需要现场考察
				AuthGroup ag=authExportGroupService.queryAuthGroupBySchoolId(as.getPid());
				if(null!=ag){
					return toResponse(null, ReturnCode.SCHOOL_HAVE_GROUP, "该机构已绑定现场考察专家组");
				}
			}
			as.setReview(review);
			authSchoolService.updateAuthSchoolInfo(as);
			return toResponse(as, ReturnCode.SUCCESS, "修改是否现场考察状态成功");
		} catch (Exception e) {
			log.info("修改是否现场考察状态失败", e);
			return toResponse(null, ReturnCode.FAIL, "修改是否现场考察状态失败");
		}
	}

	/**
	 * 删除机构
	 * 
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/deleteAuthSchoolInfo", produces = MediaType.APPLICATION_JSON_VALUE
			+ ";charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> deleteAuthSchoolInfo(
			HttpServletRequest request, @RequestParam String pid)
			throws SQLException {
		try {
			if (StringUtils.isBlank(pid)){
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL,"参数存在空值");
			}
			AuthSchool a = authSchoolService.queryAuthSchoolInfoBypid(pid);
			if (null==a) {
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "该机构不存在");
			}
			if("1".equals(a.getFpexpert())){
				return toResponse(null, ReturnCode.SCHOOL_HAVE_GROUP, "该机构已绑定现场考察专家组");
			}
			authSchoolService.deleteAuthSchoolInfo(pid);
			return toResponse(null, ReturnCode.SUCCESS, "删除机构成功");
		} catch (Exception e) {
			log.info("删除机构失败", e);
			return toResponse(null, ReturnCode.FAIL, "删除机构失败");
		}
	}
	
	/**
	 * 根据id查询机构信息
	 * 
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAuthSchoolInfoById", produces = MediaType.APPLICATION_JSON_VALUE
			+ ";charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryAuthSchoolInfoById(
			HttpServletRequest request, @RequestParam String pid)
			throws SQLException {
		try {
			if (StringUtils.isBlank(pid)){
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL,"参数存在空值");
			}
			AuthSchool a = authSchoolService.queryAuthSchoolInfoBypid(pid);
			if (a == null) {
				return toResponse(a, ReturnCode.PARAMETER_NOT_NULL, "该机构不存在");
			}
			return toResponse(a, ReturnCode.SUCCESS, "根据id查询机构信息成功");
		} catch (Exception e) {
			log.info("根据id查询机构信息失败", e);
			return toResponse(null, ReturnCode.FAIL, "根据id查询机构信息失败");
		}
	}

	
	/**
	 * 查询机构信息
	 * 
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAuthSchoolInfo", produces = MediaType.APPLICATION_JSON_VALUE
			+ ";charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryAuthSchoolInfo(
			HttpServletRequest request)
			throws SQLException {
		try {
			String pid=(String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==pid){
				return toResponse(null, ReturnCode.NOLOGIN,"未登录");
			}
			AuthSchool a = authSchoolService.queryAuthSchoolInfoBypid(pid);
			if (a == null) {
				return toResponse(a, ReturnCode.PARAMETER_NOT_NULL, "该机构不存在");
			}
			return toResponse(a, ReturnCode.SUCCESS, "查询机构信息成功");
		} catch (Exception e) {
			log.info("查询机构信息失败", e);
			return toResponse(null, ReturnCode.FAIL, "查询机构信息失败");
		}
	}
	/**
	 * 分页查询所有机构列表
	 * 
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAuthSchoolInfoByMajorPage", produces = MediaType.APPLICATION_JSON_VALUE
			+ ";charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAuthSchoolInfoByMajorName(HttpServletRequest request, @RequestBody Map<String, String> map) throws SQLException {	
		
		try {
			String pageNumber = map.get("pageNumber");
			String pageSize = map.get("pageSize");
			if (StringUtils.isBlank(pageNumber))
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "数量不能为空!");
			if (StringUtils.isBlank(pageSize))
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "页数不能为空!");
			
			Page<AuthSchool> page =authSchoolService.queryAuthSchoolPageInforBymajor(map);
			return toResponse(page, ReturnCode.SUCCESS, "分页查询机构列表成功");
		} catch (Exception e) {
			log.info("分页查询机构列表失败",e);
			return toResponse(null, ReturnCode.FAIL, "分页查询机构列表失败");
		}
	}
	
	/**
	 * 分页查询所有提交认证申请的机构列表
	 * 
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAuthSchoolInfoByMajorPage1", produces = MediaType.APPLICATION_JSON_VALUE
			+ ";charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAuthSchoolInfoByMajorName1(HttpServletRequest request, @RequestBody Map<String, String> map) throws SQLException {	
		
		try {
			String pageNumber = map.get("pageNumber");
			String pageSize = map.get("pageSize");
			if (StringUtils.isBlank(pageNumber))
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "数量不能为空!");
			if (StringUtils.isBlank(pageSize))
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "页数不能为空!");
			map.put("state", "1");
			Page<AuthSchool> page =authSchoolService.queryAuthSchoolPageInforBymajor(map);
			return toResponse(page, ReturnCode.SUCCESS, "分页查询所有提交认证申请的机构列表成功");
		} catch (Exception e) {
			log.info("分页查询所有提交认证申请的机构列表失败",e);
			return toResponse(null, ReturnCode.FAIL, "分页查询所有提交认证申请的机构列表失败");
		}
	}
	
	
	/**
	 * 修改机构信息
	 * 
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/updateAuthSchoolInfoAccount", produces = MediaType.APPLICATION_JSON_VALUE
			+ ";charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> updateAuthSchoolInfoAccount(
			HttpServletRequest request, @RequestBody Map<String, String> map)
			throws SQLException {
		try {
			String pid = map.get("pid");
			String account = map.get("account");
			String schoolname = map.get("schoolname");
			String major = map.get("major");
			if (StringUtils.isBlank(pid)||StringUtils.isBlank(account)||StringUtils.isBlank(schoolname)||StringUtils.isBlank(major)) {
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL,"单位名称信息不能为空");
			}
			AuthSchool as = authSchoolService.queryAuthSchoolInfoBypid(pid);
			if (null==as) {
				return toResponse(null, ReturnCode.USERINFO_NOT_EXIST,"该机构信息不存在");
			}
			AuthSchool as1 = authSchoolService.queryAuthSchoolInfoByAccount(account);
			if(null!=as1){
				if(!as.getPid().equals(as1.getPid())){
					return toResponse(null, ReturnCode.ACCOUNT_HAVE_EXIST,"该学校代码已存在");
				}
			}
			as.setAccount(account);
			as.setSchoolName(schoolname);
			as.setMajor(major);
			authSchoolService.updateAuthSchoolInfo(as);
			return toResponse(as, ReturnCode.SUCCESS, "修改机构信息成功");
		} catch (Exception e) {
			log.info("修改机构信息失败",e);
			return toResponse(null, ReturnCode.FAIL, "修改机构信息失败");
		}
	}
	
	/*****************************************************************************************************************/
	@RequestMapping(value = "/queryAuthSchoolInfo1", produces = MediaType.APPLICATION_JSON_VALUE
			+ ";charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryAuthSchoolInfo1(HttpServletRequest request,@RequestParam String schoolid)throws SQLException {
		try {
			AuthSchool a = authSchoolService.queryAuthSchoolInfoBypid(schoolid);
			if (a == null) {
				return toResponse(a, ReturnCode.PARAMETER_NOT_NULL, "该机构不存在");
			}
			return toResponse(a, ReturnCode.SUCCESS, "查询机构信息成功");
		} catch (Exception e) {
			log.info("查询机构信息失败", e);
			return toResponse(null, ReturnCode.FAIL, "查询机构信息失败");
		}
	}
}
